package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.*;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.PlanUnitMapper;
import com.woniuxy.factory.mapper.ProductMapper;
import com.woniuxy.factory.mapper.ProductionPlanMapper;
import com.woniuxy.factory.service.PlanUnitService;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlanUnitServiceImp implements PlanUnitService {
    @Autowired
    PlanUnitMapper planUnitMapper;
    @Autowired
    ProductionPlanMapper productionPlanMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ApplicationRecordMapper applicationRecordMapper;
    @Override
    public ResponseResult<List<Planunit>> getPlanUnit(Planunit planunit,int page,int limit) {
        ResponseResult<List<Planunit>> responseResult = new ResponseResult<>();
        //查询条数
        int count = planUnitMapper.selectCount(planunit);
        //计算当前页的第一条记录
        int home = (page-1)*limit;
        List<Planunit> planunits= planUnitMapper.selectPlanUnit(planunit,home,limit);
        responseResult.setCount(count);
        responseResult.setData(planunits);
        responseResult.setCode(AuditType.No_Approval);
        responseResult.setMsg("查询成功");
        return responseResult;
    }

    @Override
    public ResponseResult addPlanUnit(String prpId,String prdId,int pnumber) {
        //查询当前计划是否存在
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setPrpId(prpId);
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan, 0, 3);
        try {
            if (productionPlans.get(0).getApplicationRecord().getApplystatus()!=0){
                return new ResponseResult(AuditType.ALREADY_PASSED,"计划状态已修改不可新增",null,null);
            }
        }catch (NullPointerException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,"该计划不存在",null,null);
        }
        //查询商品是否存在
        Product product = new Product();
        product.setPrdId(prdId);
        List<Product> products = productMapper.selectProducts(product);
        try {
            products.get(0);
        } catch (NullPointerException e) {
            return new ResponseResult(AuditType.ALREADY_PASSED,"该商品不存在",null,null);
        }
        Planunit planunit = new Planunit();
        planunit.setPrpId(prpId);
        planunit.setProduct(product);
        planunit.setPnumber(pnumber);
        planUnitMapper.insertPlanunit(planunit);
        return new ResponseResult(AuditType.No_Approval,"新增成功",null,null);
    }

    @Override
    public ResponseResult deleteplanunits(int[] plaIds,String prpId) {
        ResponseResult responseResult = new ResponseResult();
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setPrpId(prpId);
        //查询计划状态
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan, 0, 3);
        try {
            if (productionPlans.get(0).getApplicationRecord().getApplystatus()!=0){
                responseResult.setCode(AuditType.ALREADY_PASSED);
                responseResult.setMsg("计划状态已修改不可删除");
                return responseResult;
            }
        }catch (NullPointerException e){
            responseResult.setCode(1);
            responseResult.setMsg("该计划不存在");
            return responseResult;
        }
        planUnitMapper.deleteplanunits(plaIds);
        return new ResponseResult(0,"删除成功",null,null);
    }

    @Override
    public ResponseResult<Map> getPlanUnitById(int plaId) {
        Map<String,Object> map = new HashMap<>();
        //查询所有商品信息
        List<Product> products = productMapper.selectProducts(new Product());
        //查询指定计划项
        Planunit planunit = new Planunit();
        planunit.setPlaId(plaId);
        List<Planunit> planunits = planUnitMapper.selectPlanUnit(planunit, 0, 3);
        ApplicationRecord applicationRecord = new ApplicationRecord();
        applicationRecord.setApplicationnum(planunits.get(0).getPrpId());
        List<ApplicationRecord> applicationRecords = applicationRecordMapper.selectApplicationRecordByCondition(applicationRecord);
        if (applicationRecords.get(0).getApplystatus()!=0){
            return new ResponseResult<>(1,"计划状态已修改不可删除",0,map);
        }
        map.put("products",products);
        map.put("planunits",planunits.get(0));
        return new ResponseResult<>(0,"查询成功",0,map);
    }

    @Override
    public ResponseResult updatePlanUnit(Planunit planunit,String prdId) {
        //查询当前计划是否存在
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setPrpId(planunit.getPrpId());
        //查询计划状态
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan, 0, 3);
        try {
            if (productionPlans.get(0).getApplicationRecord().getApplystatus()!=0){
                return new ResponseResult(1,"计划状态已修改不可删除",null,null);
            }
        }catch (NullPointerException e){
            return new ResponseResult(1,"该计划不存在",null,null);
        }
        planUnitMapper.updatePlanunit(planunit,prdId);
        return new ResponseResult(0,"修改成功",null,null);
    }
}
